package com.weixingtu.commuterCar.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;

import java.io.IOException;

public class AuthUtil {
    public static final String CORPID = "wxd2293ff2cd5879d5";
    public static final String APPSECRET = "wOIYOG9OoChiIzNNOmlmKYSdTAA2g6iLh9nd2eWFm6E";

    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException{
        JSONObject jsonObject = null;
//        //首先初始化HttpClient对象
////        DefaultHttpClient client = new DefaultHttpClient();
////        //通过get方式进行提交
////        HttpGet httpGet = new HttpGet(url);
////        //通过HttpClient的execute方法进行发送请求
////        HttpResponse response = client.execute(httpGet);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpGet);

        //从response里面拿自己想要的结果
        HttpEntity entity = response.getEntity();
        if(entity != null){
            String result = EntityUtils.toString(entity,"UTF-8");
            jsonObject = JSONObject.fromObject(result);
        }
        //把链接释放掉
        httpGet.releaseConnection();
        return jsonObject;
    }
}
